package com.example.choupaiapp.common.util;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.example.choupaiapp.R;


/**
 * Created by ASUS on 2018/11/21.
 */

public class DialogUtil {
    private static DialogUtil dialogUtil = null;
    public View dialog_bottom_layout;
    public View dialog_top_layout;
    public Dialog dialog_bottom;
    public Dialog dialog_top;
    public View dialog_center_layout;
    public AlertDialog dialog_center;
    public View dialog_center_edit_layout;
    public AlertDialog dialog_center_edit;


    public static DialogUtil getInstance() {
        if (dialogUtil == null) {
            dialogUtil = new DialogUtil();
        }
        return dialogUtil;
    }

    public void showTopDialog(Context context, int layout) {
        dialog_top_layout = View.inflate(context, layout, null);
        dialog_top = new Dialog(context, R.style.TopDialogStyle);
        dialog_top.setContentView(dialog_top_layout);
        Window dialogWindow = dialog_top.getWindow();
        dialogWindow.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        WindowManager m = dialogWindow.getWindowManager();
        Display d = m.getDefaultDisplay();
        dialogWindow.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.TOP);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.x = 0;
        lp.y = 0;
//        lp.height = d.getHeight();
        lp.width = d.getWidth();
        // 设置显示位置
        dialog_top.onWindowAttributesChanged(lp);
        // 设置点击外围解散
        dialog_top.setCanceledOnTouchOutside(true);
        dialog_top.show();
    }


    public void showBottomDialog(Context context, int layout) {
        dialog_bottom_layout = View.inflate(context, layout, null);
        dialog_bottom = new Dialog(context, R.style.BottomDialogStyle);
        dialog_bottom.setContentView(dialog_bottom_layout);
        Window dialogWindow = dialog_bottom.getWindow();
        WindowManager m = dialogWindow.getWindowManager();
        Display d = m.getDefaultDisplay();
        dialogWindow.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM);
        dialogWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.x = 0;
        lp.y = 0;
//        lp.height = d.getHeight();
        lp.width = d.getWidth();
        // 设置显示位置
        dialog_bottom.onWindowAttributesChanged(lp);
        // 设置点击外围解散
        dialog_bottom.setCanceledOnTouchOutside(true);
        dialog_bottom.show();
    }

    public void showCenterDialog(Context context, int layout) {
        dialog_center_layout = View.inflate(context, layout, null);
        dialog_center = new AlertDialog.Builder(context, R.style.NoBackGroundDialog).create();
        dialog_center.show();
        dialog_center.setContentView(dialog_center_layout);
    }

    /**
     * 带有edittext的dialog 因为不能输入需要单独加一行代码
     *
     * @param context
     * @param layout
     */
    public void showCenterEditDialog(Context context, int layout) {
        dialog_center_edit_layout = View.inflate(context, layout, null);
        dialog_center_edit = new AlertDialog.Builder(context, R.style.NoBackGroundDialog).create();
        dialog_center_edit.setView(((Activity) context).getLayoutInflater().inflate(layout, null));
//        dialog_center_edit.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        dialog_center_edit.show();
        dialog_center_edit.setContentView(dialog_center_edit_layout);
    }

}
